package com.neusoft.hifly.kafka.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.kafka.domain.DataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
@RequestMapping("/api/v1/kafka")
@Api(value = "kafka-api", description = "Kafka发送消息")
public class KafkaApi {
	@Autowired(required = false)
	private KafkaTemplate<String, Object> kafkaTemplate;

	/**
	 * kafka发送消息
	 *
	 * @param obj
	 *            消息
	 * @return 是否成功
	 */
	@ApiOperation("kafka发送消息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "obj", value = "消息，例如{\"topic\":\"micro-netty\",\"data\":\"你好\"}", dataType = "string", required = true) })
	@PostMapping(value = "/send")
	public ApiResponse<?> getByLogicId(@RequestBody final DataVO obj) {
		kafkaTemplate.send(obj.getTopic(), obj.getData());
		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "成功！");
	}

}
